/**
 * 
 */
package com.bdcom.model;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D.Double;
import java.util.Iterator;

import com.bdcom.util.PlannerUtil;

/**
 * @author Administrator
 *
 */
public class LineBarrier extends AbstractBarrier {

	/* (non-Javadoc)
	 * @see com.bdcom.model.AbstractBarrier#drawBarrier()
	 */
	@Override
	public void drawBarrier(Graphics2D g/*,double roompercent*/,boolean selected) {
		// TODO Auto-generated method stub
		
		int pointSize=getPoints().size();
		
		GeneralPath linePath=new GeneralPath(GeneralPath.WIND_EVEN_ODD, pointSize);
		
		Iterator<Point> iteratorPoint=getPoints().iterator();
		
		Point point;
		
		point=iteratorPoint.next();
		
		linePath.moveTo(point.x, point.y);
		
		while(iteratorPoint.hasNext()){
			
			point=iteratorPoint.next();
			
			linePath.lineTo(point.x, point.y);
			
		}
		
		if(selected){
			
			Stroke s=new BasicStroke(PlannerUtil.SELECTED_BORDERWIDTH, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
			
			g.setColor(PlannerUtil.SELECTED_BORDERCOLOR);
			
			g.setStroke(s);
		}else{

			g.setStroke(new BasicStroke(PlannerUtil.DEFAULT_BORDERWIDTH));
			
			g.setColor(Color.decode(getBarrierType().getColor()));
		}
		
		g.draw(linePath);

		//setGeneralPath(linePath);
		
	}

	@Override
	public boolean containsPoint(Point point) {
		// TODO Auto-generated method stub
		Rectangle rect=new Rectangle(point.x-2, point.y-2, 4, 4);
		
		int size=getPoints().size();
		
		Point A,B;
		
		for(int i=0;i<size-1;i++){
			
			A=getPoints().get(i);
			
			B=getPoints().get(i+1);
			
			Double line=new Double(A.getX(), A.getY(), B.getX(), B.getY());
			
			if(line.intersects(rect))
			{
				return true;
			}
		}
		return false;
	}

	@Override
	public int checkLineIntersectWithMe(double x0, double y0, double x1, double y1) {
		// TODO Auto-generated method stub
		
		int size=getPoints().size();
		
		int count=0;
		
		Point A,B;
		
		for(int i=0;i<size-1;i++){
			
			A=getPoints().get(i);
			
			B=getPoints().get(i+1);
			
			Double line=new Double(A.getX(), A.getY(), B.getX(), B.getY());
			
			if(line.intersectsLine(x0,y0,x1,y1)){
				count++;
			};
		}
		
		return count;
	}
	
}
